<uieip type="page" value="login" debug="true" timeout="5000"></uieip>
<uieip type="reg" value="console"></uieip>
<!DOCTYPE html>
<html>

<head>
    <uieip type="file" value="inc" src="/yqwin/easyui/include.txt" cache="true" minute="1440"></uieip>
    <uieip type="file" value="css" src="/yqwin/css/yqwin.min" cache="true"></uieip>
    <uieip type="file" value="js" src="/yqwin/src/yqwin.min" cache="false"></uieip>
    <title>Data Edit</title>
    <style>
       body {
            margin: 0px;
            border-color: #95B8E7;
            border-top-width: 0px;
            border-right-width: 0px;
            border-bottom-width: 0px;
            border-left-width: 1px;
            border-style: solid;
        }

        table {
            border-spacing: 0px;
        }
    </style>
</head>

<body>
    <div id="tab_set" data-options="border:false,tabPosition:'left',tabWidth:78,headerWidth:78,tabHeight:37" class="easyui-tabs" style="height:250px;">
        <div title="权限标识" style="padding:0px;display:none;">
            <table id="tb_mask"></table>
        </div>
        <div title="界面参数"  style="padding:0px;display:none;">
            <table id="tb_var"></table>
        </div>
        <div title="数据"  style="padding:0px;display:none;">
            <table id="tb_ui"></table>
        </div>
    </div>
    <script>
        var dbname = $.urlRequest("dbname");
        var menu_id = $.urlRequest("menu_id");
        
        function form_load() {
            $(window).resize(function () {
                 onresize();
            });
            onresize();
            
            tabs_clear();

            $("#tab_set").tabs("options").onSelect = function(title,index){
                tabs_select(index);
            }
            tabs_select(0);
        }

        function onresize() {
            var w = $(window).width()-1;
            var h = $(window).height();
         
            $("#tab_set").tabs("resize",{width:w,height:h});
            w=w-78;
            $("#tb_mask").yqDataGrid("resize",{width:w,height:h});
            $("#tb_var").yqDataGrid("resize",{width:w,height:h});
            $("#tb_ui").yqDataGrid("resize",{width:w,height:h});
        }

        $(function () {           
            init_mark();
        });

        function init_mark(){
            $("#tb_mask").yqDataGrid({
                ajaxUrl: "/_db.html",
                dbname: dbname,
                tbname: "sys_menu_mark",
                keyId: "sid",
                bindingKeyId: "menu_id",
                bindingValue: menu_id,
                pKeyId: null,
                pKeyValue: null,

                tempno: 1,

                addWhere: "",
                order: "queue",

                editMode: 3,
                editModeW: 600,
                editModeH: 450,
                editModeNo: 2,
                loadData: false,
                isPage: false,

                isEdit: true,
                copy: true,
                add: true,
                upp: true,
                del: true,
                isdel: false,
                isdelEdit: false,
                move: true,
 
                isSimple: false,
                isSeek: false,

                linkField: ["describe"],
                
                editLinks: [{},{
                    id: "init",
                    text: "添加基本权限标识",
                    iconCls: "icon2-init",
                }],

                uppActionState: function (s_index, s_row, callback) {     //操作节点状态处理接口
                    if(intmarkbtn_check()){
                        $("#tb_mask").yqDataGrid("btnEnable","init");
                    }else{
                        $("#tb_mask").yqDataGrid("btnDisable","init");
                    }
                    callback(true);   //返回 true ，启用右键菜单
                },
                btnAction: function (action, params,callback) {
                    if(action=="init"){
                        init_newmark();
                        callback(false);
                    } else{
                        callback(true);
                    }      
                },
 
                beforeBindingTitle: function (titles, callback) {
                    callback(titles);
                }, //标题绑定前
                afterBindingTitle: function () {
                    init_var();
                }, //标题绑定后
            });
        }

        function init_var(){
            $("#tb_var").yqDataGrid({
                ajaxUrl: "/_db.html",
                dbname: dbname,
                tbname: "sys_menu_var", 
                keyId: "sid",
                bindingKeyId: "menu_id",
                bindingValue: menu_id,
                pKeyId: null,
                pKeyValue: null,

                tempno: 1,

                addWhere: "",
                order: "queue",

                editMode: 3,
                editModeW: 600,
                editModeH: 450,
                editModeNo: 2,
                loadData: false,
                isPage: false,

                isEdit: true,
                copy: true,
                add: true,
                upp: true,
                del: true,
                isdel: false,
                isdelEdit: false,
                move: true,
 
                isSimple: false,
                isSeek: false,
 
                linkField: ["describe"],

                beforeBindingTitle: function (titles, callback) {
                    for(var i=0;i<titles.length;i++){
                        if(titles[i].name=="mark_no"){
                            titles[i].ui_mask.dbName=dbname;
                            titles[i].ui_mask.addWhere="menu_id='"+ menu_id +"' and no>0";
                            //console.log(titles[i]);
                        }
                    }
                   
                    callback(titles);
                },
                onBeforeShowEdit: function (titles, action, row, callback) { 
                    for(var i=0;i<titles.length;i++){
                        if(titles[i].name=="mark_no"){
                            titles[i].ui_mask.dbName=dbname;
                            titles[i].ui_mask.addWhere="menu_id='"+ menu_id +"' and no>0";
                            //console.log(titles[i]);
                        }
                    }
                   
                    callback(true, titles, row);
                },
                afterBindingTitle: function () {
                    init_ui();
                }, 
            });
        }

        function init_ui(){
            $("#tb_ui").yqDataGrid({
                ajaxUrl: "/_db.html",
                dbname: dbname,
                tbname: "sys_menu_ui",
                keyId: "sid",
                bindingKeyId: "menu_id",
                bindingValue: menu_id,
                pKeyId: null,
                pKeyValue: null,

                tempno: 1,

                addWhere: "",
                order: "queue",

                editMode: 3,  
                editType: 1, 
                editModeW: 600,
                editModeH: 450,
                editModeNo: 2,
                loadData: false,
                isPage: false,

                isEdit: true,
                copy: true,
                add: true,
                upp: true,
                del: true,
                isdel: false,
                isdelEdit: false,
                move: true,
 
                isSimple: false,
                isSeek: false,

                loadDefault: true,
                linkField: ["describe"],
                beforeBindingTitle: function (titles, callback) {
                    for(var i=0;i<titles.length;i++){
                        if(titles[i].name=="mark_no"){
                            titles[i].ui_mask.dbName=dbname;
                            titles[i].ui_mask.addWhere="menu_id='"+ menu_id +"' and no>0";
                            //console.log(titles[i]);
                        }
                    }
                   
                    callback(titles);
                }, 
                onBeforeShowEdit: function (titles, action, row, callback) { 
                    for(var i=0;i<titles.length;i++){
                        if(titles[i].name=="mark_no"){
                            titles[i].ui_mask.dbName=dbname;
                            titles[i].ui_mask.addWhere="menu_id='"+ menu_id +"' and no>0";
                            //console.log(titles[i]);
                        }
                    }
                   
                    callback(true, titles, row);
                },
                afterBindingTitle: function () {
                    form_load();
                }, 
            });
        }

        function init_newmark(){
            var rows = $("#tb_mask").yqDataGrid("getRows");
            var hasmark0=false;
            var hasmark1=false;
            for(var i=0;i<rows.length;i++){
                if(rows[i]["mark_type"]==0){
                    hasmark0=true;
                }else if(rows[i]["mark_type"]==1){
                    hasmark1=true;
                }
            }

            if(hasmark0 && hasmark1){
                $.yqMessager("info", "标识类型已存在。");
                return;
            }

            var a_rows =[];
            if(!hasmark0){
                a_rows.push({action:"add",row:{menu_id:menu_id,mark_type:0,no:0,describe:"菜单权限"}});
            }
            if(!hasmark1){
                a_rows.push({action:"add",row:{menu_id:menu_id,mark_type:1,no:1,describe:"界面权限"}});
            }
            $("#tb_mask").yqDataGrid("dataAction",a_rows);
        }

        function intmarkbtn_check(){
            var rows = $("#tb_mask").yqDataGrid("getRows");
            var hasmark0=false;
            var hasmark1=false;
            for(var i=0;i<rows.length;i++){
                if(rows[i]["mark_type"]==0){
                    hasmark0=true;
                }else if(rows[i]["mark_type"]==1){
                    hasmark1=true;
                }
            }
             if(hasmark0 && hasmark1){
                return false;
            }else{
                return true;
            }
        }

        function tabs_select(index){
            if(dbname == "" || menu_id == ""){
                return;
            }

            var $tab = $("#tab_set").tabs("tabs")[index];
            var opt = $tab.panel("options");
            if(opt.loaddata==undefined){
                //console.log("loaddata",index);
                if(index==0){
                    $("#tb_mask").yqDataGrid("loadData");
                } else if(index==1){
                    $("#tb_var").yqDataGrid("loadData");
                } else if(index==2){
                    $("#tb_ui").yqDataGrid("loadData");
                }
                
                opt.loaddata=true;
            }
        }

        function tabs_clear(){
            $("#tb_mask").yqDataGrid("clear");
            $("#tb_var").yqDataGrid("clear");
            $("#tb_ui").yqDataGrid("clear");

            var $tabs = $("#tab_set").tabs("tabs");
            for(var index=0;index<$tabs.length;index++){
                delete $tabs[index].panel("options").loaddata;
            }
        }
    </script>
</body>

</html>